home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / Intuition / Special / remembertest.c < prev   
C/C++ Source or Header  |  1992-09-03  |  6KB  |  177 lines

  1. ;/* remembertest.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 remembertest.c
  3. Blink FROM LIB:c.o,remembertest.o TO remembertest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. ** RememberTest - Illustrates the use of AllocRemember() and FreeRemember().
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <dos/dos.h>
  41. #include <intuition/intuition.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/intuition_protos.h>
  45.  
  46. #include <stdlib.h>
  47.  
  48. #ifdef LATTICE
  49. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  50. int chkabort(void) { return(0); }  /* really */
  51. #endif
  52.  
  53. /* our function prototypes */
  54. VOID methodOne(VOID);
  55. VOID methodTwo(VOID);
  56.  
  57. struct IntuitionBase *IntuitionBase;
  58.  
  59. /* random sizes to demonstrate the Remember functions. */
  60. #define SIZE_A 100L
  61. #define SIZE_B 200L
  62.  
  63.  
  64. /*
  65. ** main() - Initialize everything.
  66. */
  67. VOID main(int argc, char **argv)
  68. {
  69. LONG exitVal = RETURN_OK;
  70.  
  71. IntuitionBase = OpenLibrary("intuition.library", 33L);
  72. if (IntuitionBase == NULL)
  73.     exitVal = RETURN_FAIL;
  74. else
  75.     {
  76.     methodOne();
  77.     methodTwo();
  78.  
  79.     CloseLibrary(IntuitionBase);
  80.     }
  81. exit(exitVal);
  82. }
  83.  
  84.  
  85. /*
  86. ** MethodOne
  87. ** Illustrates using AllocRemember() to allocate all memory and
  88. ** FreeRemember() to free it all.
  89. */
  90. VOID methodOne(VOID)
  91. {
  92. APTR memBlockA = NULL, memBlockB = NULL;
  93. struct Remember *rememberKey = NULL;
  94.  
  95. memBlockA = AllocRemember(&rememberKey, SIZE_A, MEMF_CLEAR | MEMF_PUBLIC);
  96. if (memBlockA)
  97.     {
  98.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  99.     memBlockB = AllocRemember(&rememberKey, SIZE_B, MEMF_CLEAR | MEMF_PUBLIC);
  100.     if (memBlockB)
  101.         {
  102.         /*  Both memory allocations succeeded.
  103.         **  The program may now use this memory.
  104.         */
  105.         }
  106.     }
  107.  
  108. /* It is not necessary to keep track of the status of each allocation.
  109. ** Intuition has kept track of all successful allocations by updating its
  110. ** linked list of Remember nodes.  The following call to FreeRemember() will
  111. ** deallocate any and all of the memory that was successfully allocated.
  112. ** The memory blocks as well as the link nodes will be deallocated because
  113. ** the "ReallyForget" parameter is TRUE.
  114. **
  115. ** It is possible to have reached the call to FreeRemember()
  116. ** in one of three states.  Here they are, along with their results.
  117. **
  118. ** 1. Both memory allocations failed.
  119. **       RememberKey is still NULL.  FreeRemember() will do nothing.
  120. ** 2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  121. **       FreeRemember() will free the memory block pointed to by memBlockA.
  122. ** 3. Both memory allocations were successful.
  123. **       FreeRemember() will free the memory blocks pointed to by
  124. **       memBlockA and memBlockB.
  125. */
  126. FreeRemember(&rememberKey, TRUE);
  127. }
  128.  
  129. /*
  130. ** MethodTwo
  131. ** Illustrates using AllocRemember() to allocate all memory,
  132. ** FreeRemember() to free the link nodes, and FreeMem() to
  133. ** free the actual memory blocks.
  134. */
  135. VOID methodTwo(VOID)
  136. {
  137. APTR memBlockA = NULL, memBlockB = NULL;
  138. struct Remember *rememberKey = NULL;
  139.  
  140. memBlockA = AllocRemember(&rememberKey, SIZE_A, MEMF_CLEAR | MEMF_PUBLIC);
  141. if (memBlockA)
  142.     {
  143.     /*  The memBlockA allocation succeeded; try for memBlockB.  */
  144.     memBlockB = AllocRemember(&rememberKey, SIZE_B, MEMF_CLEAR | MEMF_PUBLIC);
  145.     if (memBlockB)
  146.         {
  147.         /* Both memory allocations succeeded.
  148.         ** For the purpose of illustration, FreeRemember() is called at
  149.         ** this point, but only to free the link nodes.  The memory pointed
  150.         ** to by memBlockA and memBlockB is retained.
  151.         */
  152.         FreeRemember(&rememberKey, FALSE);
  153.  
  154.         /* Individually free the two memory blocks. The Exec FreeMem()
  155.         ** call must be used, as the link nodes are no longer available.
  156.         */
  157.         FreeMem((VOID *)memBlockA, SIZE_A);
  158.         FreeMem((VOID *)memBlockB, SIZE_B);
  159.         }
  160.     }
  161.  
  162. /* It is possible to have reached the call to FreeRemember()
  163. ** in one of three states.  Here they are, along with their results.
  164. **
  165. ** 1. Both memory allocations failed.
  166. **    RememberKey is still NULL.  FreeRemember() will do nothing.
  167. ** 2. The memBlockA allocation succeeded but the memBlockB allocation failed.
  168. **    FreeRemember() will free the memory block pointed to by memBlockA.
  169. ** 3. Both memory allocations were successful.
  170. **    If this is the case, the program has already freed the link nodes
  171. **    with FreeRemember() and the memory blocks with FreeMem().
  172. **    When FreeRemember() freed the link nodes, it reset RememberKey
  173. **    to NULL.  This (second) call to FreeRemember() will do nothing.
  174. */
  175. FreeRemember(&rememberKey, TRUE);
  176. }
  177.